package gov.ccswj.modules.apibusiness.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gov.ccswj.modules.api.annotation.AuthIgnore;
import gov.ccswj.modules.apibusiness.service.InfoMonitorService;
import gov.ccswj.modules.apibusiness.service.PreciseInvestmentService;
import gov.ccswj.modules.apibusiness.utils.ApiR;

/**
 * @author KY.Cheng
 * 2.4 信息监测系统-接口
 */
@RestController
@RequestMapping("/api/infomonitor")
public class InfoMonitorController {

	@Autowired
	private InfoMonitorService infoMonitorService;
	@Autowired
	private PreciseInvestmentService preciseInvestmentService;

	/**
	 * 2.1.1.1同期与行业平均水平数据的比对
	 */
	@AuthIgnore
	@GetMapping("/comparisonbyavg")
	public ApiR comparisonbyavg(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = comparisonbyavg();
		return ApiR.ok().put("data", map);
	}
	public Map<String, Object> comparisonbyavg(){
		List<Map<String, Object>> list = infoMonitorService.queryComparison("2017");

		Map<String, Object> map = new HashMap<String, Object>();
		List<String> datax = new ArrayList<>();
		List<Map<String, Object>> datay = new ArrayList<>();
		Map<String, Object> dataym1 = new HashMap<String, Object>();
		Map<String, Object> dataym2 = new HashMap<String, Object>();
		List<String> datayl1 = new ArrayList<>();
		List<String> datayl2 = new ArrayList<>();
		for (Map<String, Object> mm : list) {
			datax.add((String) mm.get("name"));
			datayl1.add((String) mm.get("data_cc"));
			datayl2.add((String) mm.get("data_avg"));
		}
		dataym1.put("name", "本地数据");
		dataym1.put("data", datayl1);
		dataym2.put("name", "行业平均");
		dataym2.put("data", datayl2);
		datay.add(dataym1);
		datay.add(dataym2);
		map.put("datax", datax);
		map.put("datay", datay);
		return map;
	}
	/**
	 * 2.1.1.2与往年同行业同地区数据的比对
	 */
	@AuthIgnore
	@GetMapping("/comparisonbyear")
	public ApiR comparisonbyear(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = comparisonbyear();
		
		return ApiR.ok().put("data", map);
	}
	public Map<String, Object> comparisonbyear(){
		List<Map<String, Object>> list1 = infoMonitorService.queryComparison("2017");
		List<Map<String, Object>> list2 = infoMonitorService.queryComparison("2016");

		Map<String, Object> map = new HashMap<String, Object>();
		List<String> datax = new ArrayList<>();
		List<Map<String, Object>> datay = new ArrayList<>();
		Map<String, Object> dataym1 = new HashMap<String, Object>();
		Map<String, Object> dataym2 = new HashMap<String, Object>();
		List<String> datayl1 = new ArrayList<>();
		List<String> datayl2 = new ArrayList<>();
		for (Map<String, Object> mm : list1) {
			datax.add((String) mm.get("name"));
			datayl1.add((String) mm.get("data_cc"));
		}
		for (Map<String, Object> mm : list2) {
			datayl2.add((String) mm.get("data_cc"));
		}
		dataym1.put("name", "今年");
		dataym1.put("data", datayl1);
		dataym2.put("name", "往年");
		dataym2.put("data", datayl2);
		datay.add(dataym1);
		datay.add(dataym2);
		map.put("datax", datax);
		map.put("datay", datay);
		return map;
	}
	
	/**
	 * 2.1.1.3 商品数据结构分析
	 */
	@AuthIgnore
	@GetMapping("/productanalysis")
	public ApiR productanalysis(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		List<Map<String, Object>> thisYear = infoMonitorService.queryProductanalysis("2017");
		List<Map<String, Object>> previousYears = infoMonitorService.queryProductanalysis("2016");
		
		return ApiR.ok().put("thisYear", thisYear).put("previousYears", previousYears);
	}
	
	/**
	 * 2.1.1.4 企业数量统计
	 */
	@AuthIgnore
	@GetMapping("/companycount")
	public ApiR companycount(String time, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		if (StringUtils.isBlank(time)) {
			return ApiR.ok().put("status", "108").put("message", "time参数不能为空");
		}

		List<Map<String, Object>> list = infoMonitorService.companycount(time);
		return ApiR.ok().put("data", list);
	}
	
	/**
	 * 2.1.1.5 企业列表
	 */
	@AuthIgnore
	@GetMapping("/companylist")
	public ApiR companylist(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		List<Map<String, Object>> list = infoMonitorService.querycompanylist();

		return ApiR.ok().put("message", "查询成功").put("total", list.size()).put("data", list);
	}
	
	/**
	 * 2.1.1.6企业详情
	 */
	@AuthIgnore
	@GetMapping("/companyinfo")
	public ApiR companyinfo(String id, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		
		if (StringUtils.isBlank(id)) {
			return ApiR.ok().put("status", "108").put("message", "id不能为空");
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> companyinfo = infoMonitorService.querycompany(id);
		map.put("companyinfo", companyinfo);
		map.put("cid", id);

		if (null == companyinfo) {
			return ApiR.ok().put("status", "101").put("message", "id不存在");
		}
		String cname = companyinfo.get("company").toString();
		if (StringUtils.isBlank(cname)) {
			return ApiR.ok().put("status", "101").put("message", "id不存在");
		}
		
		map.put("changeMsg", infoMonitorService.querycompanyByTable("b_i_company_changeMsg", cname));
		map.put("contributionMsg", infoMonitorService.querycompanyByTable("b_i_company_contribution", cname));
		map.put("ShareholderMsg", infoMonitorService.querycompanyByTable("b_i_company_shareholder", cname));
		map.put("managementMsg", infoMonitorService.querycompanyByTable("b_i_company_management", cname));
		map.put("ShareholderPayMsg", infoMonitorService.querycompanyByTable("b_i_company_shareholderPay", cname));
		map.put("guaranteeMsg", infoMonitorService.querycompanyByTable("b_i_company_guarantee", cname));
		map.put("force", infoMonitorService.querycompanyByTable("b_i_company_force", cname));
		map.put("honor", infoMonitorService.querycompanyByTable("b_i_company_patent", cname));
		map.put("legalPerson", infoMonitorService.querycompanyByTable("b_i_company_legalPerson", cname));
		map.put("trademark", infoMonitorService.querycompanyByTable("b_i_company_trademark", cname));
		map.put("works", infoMonitorService.querycompanyByTable("b_i_company_works", cname));
		
		Map<String, Object> publicMsg = new HashMap<String, Object>();
		publicMsg.put("litigation", infoMonitorService.querycompanyByTable("b_i_company_litigation", cname));
		publicMsg.put("administration", infoMonitorService.querycompanyByTable("b_i_company_administration", cname));
		publicMsg.put("publicSentiment", infoMonitorService.querycompanyByTable("b_i_company_publicSentiment", cname));
		publicMsg.put("companyDynamics", infoMonitorService.querycompanyByTable("b_i_company_dynamics", cname));
		map.put("publicMsg", publicMsg);

		return ApiR.ok(map).put("message", "查询成功");
	}

	/**
	 * 2.1.1.7销售量和销售总额统计
	 */
	@AuthIgnore
	@GetMapping("/totalsalesstatistics")
	public ApiR totalsalesstatistics(String time, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		if (StringUtils.isBlank(time)) {
			return ApiR.ok().put("status", "108").put("message", "time参数不能为空");
		}

		List<Map<String, Object>> list = infoMonitorService.queryTotalsalesstatistics(time);
		return ApiR.ok().put("data", list);
	}

	/**
	 * 2.1.1.8 详细商品销售情况
	 */
	@AuthIgnore
	@GetMapping("/productsalesdtails")
	public ApiR productSalesDtails(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		List<Map<String, Object>> list = infoMonitorService.queryProductSalesDtails();

		return ApiR.ok().put("message", "查询成功").put("total", list.size()).put("data", list);
	}

	/**
	 * 2.1.1.9 生产资料和生活资料价格
	 */
	@AuthIgnore
	@GetMapping("/productionmaterials")
	public ApiR productionMaterials(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");

		List<Map<String, Object>> list = infoMonitorService.queryProductionArticle();
		List<Map<String, Object>> list2 = infoMonitorService.queryProductionMaterials();

		Map<String, Object> line = new HashMap<String, Object>();
		List<Object> datax = new ArrayList();
		List<Object> datay = new ArrayList();
		for (Map<String, Object> map : list2) {
			datax.add(map.get("monthday"));
			datay.add(map.get("price"));
		}
		line.put("datax", datax);
		line.put("datay", datay);
		return ApiR.ok().put("list", list).put("line", line);
	}


	/**
	 * 2.1.2 垄断舆论
	 */
	@AuthIgnore
	@GetMapping("/publicopinion")
	public ApiR publicopinion(HttpServletResponse response,String industry,String time,Integer s,Integer n) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		if (StringUtils.isBlank(time)) {
			time = "2";
		}
		if (s==null||n==null) {
			return ApiR.ok().put("status", "108").put("message", "s，n参数有误");
		}

		List<Map<String, Object>> list = infoMonitorService.queryPublicopinion(time, s, n);
		
		return ApiR.ok().put("total", list.size()).put("data", list);
	}
	
	/**
	 * 2.1.3垄断分析
	 */
	@AuthIgnore
	@GetMapping("/monopolyanalysis")
	public ApiR monopolyanalysis(HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("china", industry());
		map.put("price", comparisonbyear());
		map.put("sameLine", comparisonbyear());
		
		return ApiR.ok().put("data", map);
	}
	public Map<String, Object> industry(){
		List<Map<String, Object>> list = preciseInvestmentService.industry();
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map1 = new HashMap<String, Object>();
			map1.put("name", list.get(i).get("title").toString());
			map1.put("child", list.get(i).get("child_id").toString());
			map.put(switchWord(i), map1);
		}
		Map<String, Object> link1 = new HashMap<String, Object>();
		link1.put("source", "能源电力");
		link1.put("target", "新能源");
		Map<String, Object> link2 = new HashMap<String, Object>();
		link2.put("source", "能源电力");
		link2.put("target", "石油");
		Map<String, Object> link3 = new HashMap<String, Object>();
		link3.put("source", "能源电力");
		link3.put("target", "太阳能");
		Map<String, Object> link4 = new HashMap<String, Object>();
		link4.put("source", "能源电力");
		link4.put("target", "煤炭");
		List<Map<String, Object>> links = new ArrayList<Map<String,Object>>() ;
		links.add(link1);
		links.add(link2);
		links.add(link3);
		links.add(link4);
		Map<String, Object>mm = new HashMap<String, Object>();
		mm.put("data", map);
		mm.put("links", links);
		return mm;
	}
	public String switchWord(int i){
		switch(i){
		case(0): return "one";
		case(1): return "two";
		case(2): return "three";

		default : return "";
		}
	}
}
